package com.iqiyi.pps.epg.core.service.ticket;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.ticket.*;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.ticket.TicketDataDao;
import com.iqiyi.pps.epg.core.dao.ticket.TicketLiveCheckDao;
import com.iqiyi.pps.epg.core.dao.ticket.TicketLogDao;
import com.iqiyi.pps.epg.core.dao.ticket.TicketSysConfigDao;
import com.iqiyi.pps.epg.core.model.ticket.TicketData;
import com.iqiyi.pps.epg.core.model.ticket.TicketLiveCheck;
import com.iqiyi.pps.epg.core.model.ticket.TicketLog;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by karkarrotxu on 2017/9/1.
 */
@Service
@Transactional
public class TicketServiceImpl implements ITicketService {
    private static final Logger logger = LoggerFactory.getLogger(TicketServiceImpl.class);
    @Autowired
    private TicketDataDao ticketDataDao;

    @Autowired
    private TicketLiveCheckDao ticketLiveCheckDao;

    @Autowired
    private TicketLogDao ticketLogDao;


    @Autowired
    private TicketSysConfigDao sysConfigDao;

    @Override
    public long saveLog(TicketLog entity) {
        ticketLogDao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TTicketLogPage findLogPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<TicketLog> dbPage = TransUtil.buildPage(page);
        Page<TicketLog> pageData = ticketLogDao.findPage(dbPage, filterList);
        List<TicketLog> dataList = pageData.getResult();
        TTicketLogPage retPage = new TTicketLogPage();
        page.setTotalCount(pageData.getTotalCount());
        retPage.setTPage(page);
        List<TTicketLog> list = ticketLogDao.transToTList(dataList);
        retPage.setResult(list);
        return retPage;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TTicketDataPage findTicketDataPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<TicketData> dbPage = TransUtil.buildPage(page);
        Page<TicketData> pageData = ticketDataDao.findPage(dbPage, filterList);
        List<TicketData> dataList = pageData.getResult();
        TTicketDataPage retPage = new TTicketDataPage();
        page.setTotalCount(pageData.getTotalCount());
        retPage.setTPage(page);
        List<TTicketData> list = ticketDataDao.transToTList(dataList);
        retPage.setResult(list);
        return retPage;
    }

    @Override
    public long addTicketData(TicketData entity) {
        ticketDataDao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    public void editTicketData(TicketData entity) {
        ticketDataDao.save(entity);
    }

    @Override
    public void updateOaByOaDoc(String oaDocNo, String oaEditStatus) {
        String hql = "update TicketData set oaDocEditorStatus = ? where oaDocNo = ?";
        int executeUpdate = ticketDataDao.getSession().createQuery(hql)
                .setString(0, oaEditStatus)
                .setString(1, oaDocNo).executeUpdate();
        logger.info("[updateOaByOaDoc][docNo={}][docEditorStatus={}][executeUpdate={}]", oaDocNo, oaEditStatus, executeUpdate);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<TicketData> findTicketDataList(TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        return ticketDataDao.find(filterList);
    }

    @Override
    public TicketData findTicketDataId(long id) {
        return ticketDataDao.get(id);
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<TicketLiveCheck> findLiveCheckList(TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        return ticketLiveCheckDao.find(filterList);
    }

    @Override
    public TicketLiveCheck findTicketLiveCheckByDataId(long dataId) {
        return ticketLiveCheckDao.findUniqueBy("dataId", dataId);
    }

    @Override
    public List<TTicketSysConfig> filterConfigs(TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        return sysConfigDao.transToTList(sysConfigDao.find(filterList));
    }

    @Override
    public void updateLiveCheck(TicketLiveCheck entity) {
        if (entity.getId() == 0) {
            ticketLiveCheckDao.saveOnly(entity);
        } else {
            ticketLiveCheckDao.save(entity);
        }
    }

    @Override
    public List<TTicketSysConfig> getAllConfigs() {
        Criteria criteria = sysConfigDao.createCriteria();
        return sysConfigDao.transToTList(sysConfigDao.find(criteria));
    }
}
